companyNameObj = {};
Company = {
  init() {
    Company.jump();
    Company.bindEvent();

    Company.getCompanyData();
  },
  jump() {
    $("#back").click(function () {
      history.back();
    });
  },
  bindEvent() {
    $(".company-position").click(function () {
      $(".company-message").removeClass("bb");
      $(this).addClass("bb");
      $(".work").show();
      $(".company-details").hide();
    });
    $(".company-message").click(function () {
      $(".company-position").removeClass("bb");
      $(this).addClass("bb");
      $(".work").hide();
      $(".company-details").show();
    });
  },
  //获取公司信息
  getCompanyData() {
    var webUrl = location.href;
    var index = webUrl.indexOf("=");
    var id = webUrl.slice(index + 1);
    $axios.get("/enterprise/" + id).then((res) => {
      companyNameObj = res.data;
      Company.renderCompany(res.data);
      Company.getPositionData();
    });
  },
  renderCompany(data) {
    $(".company-name").html(`
         <p class="f22 fw-500">${data.companyName}</p>
        <p><img class="logo" src="${data.imgUrl}" alt=""></p>
        `);
    $(".company-info").html(`
        <p class="c666"><span>需招${data.peopleNum}人</span> <span class="ml-15">${data.workType}</span></p>
            <p class="sc mt-20"><span>${data.welfare}</span> <span class="ml-15">${data.welfare2}</span><span class="ml-15">${data.welfare3}</span></p>
        `);
    $(".company-details").html(`
    <p class="f18 fw-500">公司简介</p>
            <p class="c666 mt-20 lh15">${data.companyDetail}</p>
            <p class="mt-20 f18">公司地址</p>
            <p class="mt-10 f14"><i class="iconfont icon-mudedi"></i><span class="ml-10">${data.address}</span></p>
    `);
  },

  //   获取职位信息
  getPositionData() {
    var url = `http://localhost:3000/positionDataList?q=${companyNameObj.companyName}`;
    $axios.get(url).then((res) => {
      Company.renderPosition(res.data);
      Company.addBrowsing(res.data);
    });
  },
  renderPosition(list) {
    var htmlStr = ``;
    list.forEach((item) => {
      
      htmlStr += `
        <a  href="../job-details/job-details.html?id=${item.id}" class="work-list flex bg-fff w100p bb3">
              <ul class="list w100p">
         <li data-id="${item.id}"  class="item w100p ">
                  <p class="work-name flex jc-sb">
                    <span class="work-title" >${item.workTitle}</span>
                  </p>
                  <p class="flex jc-sb">
                    <span class="sc">${item.price}</span>
                    <span class="f12 c888">${item.time}</span>
                  </p>
                  <p class="flex jc-sb aic">
                    <span class="condition fcc f12 ">${item.welfare}</span>
                    <span class="condition fcc f12 ">${item.welfare2}</span>
                    <span class="condition fcc f12 ">${item.welfare3}</span> 
                  </p>
                 <p>
                <span class="f14 fcc">${item.companyName}</span>
                </p>
                </li>
                </ul>
            </a>
        `; 
    });
    $(".work").html(htmlStr);
    $(".company").append(`
        <span class="sc ml-5">${list.length}</span>
        `);
  },
  // 添加浏览记录
  addBrowsing(browsingList) {
    var userId = localStorage.userId;
    if (!userId) {
      return false;
    }
    
    var itemList = [];
    browsingList.forEach(function(item) {
      itemList.push(item);
    });
    // 使用事件委托，将点击事件绑定到父元素上
    $('.work').on('click', '.list', function(event) {
      // 获取点击的元素的父元素 <li>
      var li = $(event.target).closest('li');
      // 从 data-id 属性中获取 item 的 id
      var id = li.data('id');
      // 查找对应的 item
      var item = browsingList.find(function(item) {
        return item.id === id;
      });
      if (item) {
        var data = {
          imgUrl: item.imgUrl,
          workTitle: item.workTitle,
          price: item.price,
          time: item.time,
          positionId: item.id,
          userId: localStorage.userId,
          companyName: item.companyName,
          jobDetails: item.jobDetails,
          conditionAddress: item.conditionAddress,
          conditionExperience: item.conditionExperience,
          conditionEdu: item.conditionEdu
        };
        var url = 'http://localhost:3000/browsing';
        axios.post(url, data).then(function(res) {
          console.log(res.data);
        });
      }
    });
  }
};
Company.init();
